﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elight.Entity.Ex;
using Elight.Logic.Ex;
using Elight.Utility.Format;
using Elight.Utility.ResponseModels;
using Elight.WebUI.Controllers;
using Elight.WebUI.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Elight.WebUI.Areas.Ex.Controllers
{
    [Area("Ex")]
    [LoginChecked]
    public class ExcelController : BaseController
    {
        private ExExcelLogic exExcelLogic;
        public ExcelController()
        {
            exExcelLogic = new ExExcelLogic();
        }

        [HttpGet, AuthorizeChecked]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost, AuthorizeChecked]
        public ActionResult Index(int pageIndex, int pageSize, string keyWord)
        {
            int totalCount = 0;
            List<ExExcel> pageData = exExcelLogic.GetList(pageIndex, pageSize, keyWord, ref totalCount); 
            var result = new LayPadding<ExExcel>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        [HttpGet, AuthorizeChecked]
        public ActionResult Form()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetForm(string primaryKey)
        {
            ExExcel entity = exExcelLogic.GetForm(primaryKey); 
            return Content(entity.ToJson());
        }

        [HttpPost, AuthorizeChecked]
        public ActionResult Form(ExExcel model)
        { 
            if (string.IsNullOrEmpty(model.Id))
            { 
                var item = exExcelLogic.Insert(model).ToString();
                return item != null ? Success() : Error();
            }
            else
            { 
                int row = exExcelLogic.Update(model);
                return row > 0 ? Success() : Error();
            }
        }
    }
}
